package com.yunhe.fountain.controller;

import com.yunhe.common.model.ResultObject;
import com.yunhe.fountain.constant.FountainConstant;
import com.yunhe.fountain.domain.Fountain;
import com.yunhe.fountain.service.FountainControlService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author zhengbo
 * @date 2021/7/8 10:25
 */
@RestController
@RequestMapping("/fountainControl")
public class FountainControlController {

    @Autowired
    FountainControlService fountainControlService;

    @PostMapping(value = "/sendCommand")
    public ResponseEntity<ResultObject> sendCommand(@RequestBody Fountain fountain) {
        ResultObject resultObject = new ResultObject();
        String result = fountainControlService.sendCommand(fountain);

        String resultTitle = FountainConstant.RESULT_SUCCESS_CONSTANT.getTitle(result);
        if (StringUtils.isNotEmpty(resultTitle)) {
            resultObject.setResults(resultTitle);
            resultObject.setErrorCode(0);
        } else {
            resultTitle = FountainConstant.RESULT_FAIL_CONSTANT.getTitle(result);
            resultObject.setErrorMsg(resultTitle);
            resultObject.setErrorCode(1);
        }
        return new ResponseEntity<>(resultObject, HttpStatus.OK);
    }

    @PostMapping(value = "/establishConnection")
    public ResponseEntity<ResultObject> establishConnection(@RequestBody Fountain fountain) throws IOException {
        Fountain result = fountainControlService.establishConnection(fountain);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }
}
